<?php

namespace App\Models;

class UsersAddrModel
{
    /**
     * 获取用户收获地址列表
     * entry uid
     * return array
     * */
    public static function getUserAddrList($conn,$uid){
        $addrSql="SELECT `id`, `uid`, `address`, `detailed_address`,`mobile`,`name`, `isdefault`, `time`, `status` FROM `users_addr` WHERE `uid` = $uid and `status` =1";
        return $conn->readConn($addrSql,true);
    }
    /**
     * 获取用户默认收货地址
     * entry uid
     * return array
     * */
    public static function getUserAddrDefault($conn,$uid){
        $addrSql="SELECT `id`, `uid`, `address`, `detailed_address`,`mobile`,`name`, `isdefault`, `time`, `status` FROM `users_addr` WHERE `uid` = $uid and `isdefault` =1 and `status` =1 limit 1";
        return $conn->readConn($addrSql);
    }
    /**
     * 获取用户收获地址
     * entry UserAddrExample
     * return array
     * */
    public static function getUserAddr($conn,$UserAddrExample){
        $addrSql="SELECT `id`, `uid`, `address`, `detailed_address`,`mobile`,`name`, `isdefault`, `time`, `status` FROM `users_addr` WHERE `id` = $UserAddrExample->id and `uid` = $UserAddrExample->uid";
        return $conn->readConn($addrSql);
    }
    /**
     * 用户新增收获地址
     * return array
     * */
    public static function insertUserAddr($conn,$UserAddrExample){
        $insetSql="INSERT INTO `users_addr`(`uid`,`mobile`,`name`,`detailed_address`) VALUES ($UserAddrExample->uid,'$UserAddrExample->mobile','$UserAddrExample->name','$UserAddrExample->detailed_address')";
        return $conn->executeConn($insetSql);
    }
    /**
     * 初始化当前用户收货地址
     * entry UserAddrExample
     * return array
     * */
    public static function setUserAddrInitialization($conn,$UserAddrExample){
        $upAllAddrSql="UPDATE `users_addr` SET `isdefault`=0 WHERE `uid` = $UserAddrExample->uid";
        return $conn->executeConn($upAllAddrSql);
    }
    /**
     * 修改用户收货地址
     * entry UserAddrExample
     * return int
     * */
    public static function editUserAddrInside($conn,$UserAddrExample){
        $joinSql="";
        if($UserAddrExample->mobile !=null){
            $joinSql.=" `mobile` = '$UserAddrExample->mobile',";
        }
        if($UserAddrExample->name !=null){
            $joinSql.=" `name` = '$UserAddrExample->name',";
        }
        if($UserAddrExample->address !=null){
            $joinSql.=" `address` = '$UserAddrExample->address',";
        }
        if($UserAddrExample->isdefault !=null){
            $joinSql.=" `isdefault` = '$UserAddrExample->isdefault',";
        }
        if($UserAddrExample->status !=null){
            $joinSql.=" `status` = $UserAddrExample->status,";
        }
        if($joinSql !=null){
            $innerJoin = rtrim($joinSql,",");
            $upUserSql="UPDATE  `users_addr` SET $innerJoin WHERE `id` = '$UserAddrExample->id'";
            return $conn->executeConn($upUserSql);
        }
    }
}